package library.operation;

import library.Book;
import library.ListBook;

import java.util.Scanner;
//删除操作类
public class DelOperation implements IOperation{
    @Override
    public void work(ListBook listBook) {
        System.out.print("请输入删除图书的序号:");
        Scanner scanner = new Scanner(System.in);
        int index = scanner.nextInt();
        int size = listBook.getSize();
        //注意输入的序号是否合法
        if(index < 0 && index >= size){
            System.out.println("输入的序号非法！");
            return;
        }
        //注意删除的书籍是否是最后一本书
        if(index == size - 1){
            listBook.setSize(size -1);
            System.out.println("删除图书成功！");
            return;
        }
        //记录最后一本书
        Book book = listBook.getBook(size -1);
        //将最后一本书覆盖要删除的书
        listBook.setBook(index, book);
        //删除后，书的总量减少1本
        listBook.setSize(size -1);
        System.out.println("删除图书成功！");
    }
}
